/**
 * @file InetAddress.cc
 * @brief 网络地址封装类的实现
 */

#include "InetAddress.hpp"
#include <cstring>
#include <arpa/inet.h>


namespace wd
{
    
// 从IP和端口构造地址
InetAddress::InetAddress(unsigned short port, const string & ip)
{
    memset(&_addr, 0, sizeof(_addr));
    _addr.sin_family = AF_INET;
    _addr.sin_port = htons(port);
    _addr.sin_addr.s_addr = inet_addr(ip.c_str());

}

// 从sockaddr_in结构构造
InetAddress::InetAddress(struct sockaddr_in addr)
: _addr(addr)
{

}

// 获取IP地址字符串    
string InetAddress::ip() const
{
    return string(inet_ntoa(_addr.sin_addr));
}

// 获取端口号
unsigned short InetAddress::port() const
{
    return ntohs(_addr.sin_port);
}

}//end of namespace wd
